/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.t2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import javax.jcr.*;
import javax.jcr.nodetype.*;
import javax.jcr.query.*;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author Administrator
 */
public class ILookImportFileSQL2Except {

    public static void main(String[] args) throws Exception {
        Repository repository = new TransientRepository();
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));
        try {
            Node root = session.getRootNode();

            // Store content

            mynode(session.getWorkspace());
            // Remove content


            session.save();
        } finally {
            session.logout();
        }
    }

    public static void mynode(Workspace wk) throws Exception {
        NodeTypeManager ntm = wk.getNodeTypeManager();
        NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();

        String tagName = "mytag";

        ntt.setName(tagName);
        ntt.setDeclaredSuperTypeNames(new String[]{"nt:unstructured"});
        //ntt.setPrimaryItemName("nt:unstructured");
        ntm.registerNodeType(ntt, true);
        System.out.println(ntm.hasNodeType(tagName));
        //Node node = wk.getNamespaceRegistry().registerNamespace(, null)


        Node root = wk.getSession().getRootNode();


        QueryManager qm = wk.getQueryManager();

         Node folder = root.addNode("filertf", "nt:folder");
            File file = new File("/zwu/docs/test1.txt");
            importFile(folder, file, "text/plain", "utf-8");
            wk.getSession().save();

        //Query q = qm.createQuery("select first from  mytag where first = 'zhou'", Query.SQL);
        Query q = qm.createQuery("select[jcr:data] from  [nt:resource] as file where contains(file.*, 'zhou')", Query.JCR_SQL2);
        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();
            System.out.println(r.getValues()[0].getString());
            Value excerpt = r.getValue("rep:excerpt([jcr:data])");
            System.out.println(excerpt.getString());
        }
    }

     public static Node importFile(Node folderNode, File file, String mimeType,
            String encoding) throws RepositoryException, IOException {
        //create the file node - see section 6.7.22.6 of the spec
        Node fileNode = folderNode.addNode(file.getName(), "nt:file");

        //create the mandatory child node - jcr:content
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr:mimeType", mimeType);
        resNode.setProperty("jcr:encoding", encoding);
        resNode.setProperty("jcr:data", new FileInputStream(file));
        //Node metaNode = fileNode.addNode("meta");
        //fileNode.setProperty("title", "do it");
        Calendar lastModified = Calendar.getInstance();
        lastModified.setTimeInMillis(file.lastModified());
        resNode.setProperty("jcr:lastModified", lastModified);

        return fileNode;
    }
}
